package com.zhuiYun.project.api.shoppingcart.service.Impl;

import com.zhuiYun.project.api.commoditydetail.entity.Commodity;
import com.zhuiYun.project.api.commoditydetail.mapper.CommodityMapper;
import com.zhuiYun.project.api.handworkdiy.entity.HandworkDiy;
import com.zhuiYun.project.api.handworkdiy.mapper.HandworkDiyMapper;
import com.zhuiYun.project.api.shoppingcart.entity.ShoppingCartEntity;
import com.zhuiYun.project.api.shoppingcart.mapper.ShoppingCartMapper;
import com.zhuiYun.project.api.shoppingcart.model.ShoppingCartModel;
import com.zhuiYun.project.api.shoppingcart.service.ShoppingCartService;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.ObjectUtils;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.util.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ShoppingCartServiceImpl
 * @descriptions: 购物车Service实现类
 * @Author: zq
 * @Date 2023/4/5 21:02
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    ShoppingCartMapper shoppingCartMapper;
    @Resource
    CommodityMapper commodityMapper;
    @Resource
    HandworkDiyMapper handworkDiyMapper;

    /**
     * @Author 赵笛
     * @Description TODO 商品加入购物车
     * @Date 2023/4/6 20:06
     * @Return
     * @param shoppingCartModel
     */
    @Override
    public CommonResult saveShoppingCart(ShoppingCartModel shoppingCartModel) throws CommonException {
        // 判断参数是否为空
        if (ObjectUtils.isNotEmpty(shoppingCartModel)) {
            ShoppingCartEntity shoppingCartEntity = new ShoppingCartEntity();
            // shoppingCartModel 对象拷贝成 shoppingCartEntity
            BeanUtils.copy(shoppingCartModel, shoppingCartEntity);
            //判断添加的是商品还是diy
            if(shoppingCartEntity.getIsNoDiy()==0){
                //商品
                Commodity commodity = commodityMapper.selectByPrimaryKey(shoppingCartEntity.getCommodityId());
                //判断一下当前商品是否存在
                if(ObjectUtils.isNotEmpty(commodity)){
                    //先查询一下当前商品是否已经加入购物车
                    ShoppingCartEntity shoppingCart = shoppingCartMapper.getShoppingCartByCommodityId(shoppingCartEntity.getCommodityId(),shoppingCartEntity.getUserId());
                    int i = 0;
                    if(ObjectUtils.isNotEmpty(shoppingCart)){
                        //已存在该商品，只添加数量，不生成新订单
                        i = shoppingCartMapper.updateNumberByCommodityId(
                                shoppingCart.getNumber()+shoppingCartEntity.getNumber()
                                ,shoppingCartEntity.getCommodityId()
                                ,shoppingCartEntity.getUserId());
                    }else{
                        //不存在该商品，生成新订单
                        shoppingCartEntity.setAddToCartDate(new Date());
                        //商品总价
                        shoppingCartEntity.setTotalPrices(commodity.getPrice().multiply(new BigDecimal(shoppingCartEntity.getNumber())));
                        shoppingCartEntity.setDelFlag(0);
                        shoppingCartEntity.setFlag("true");
                        shoppingCartEntity.setCreateTime(new Date());
                        shoppingCartEntity.setUpdateTime(new Date());
                        i = shoppingCartMapper.insertSelective(shoppingCartEntity);
                    }
                    if (i != 0) {
                        return CommonResult.ok(EmErrorCode.SAVE_OK);
                    } else {
                        return CommonResult.error(EmErrorCode.SAVE_ERROR);
                    }

                }else{
                    return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
                }
            }else{
                //DIY
                HandworkDiy handworkDiy = handworkDiyMapper.selectByPrimaryKey(shoppingCartEntity.getDiyId());
                //判断一下当前DIY是否存在
                if(ObjectUtils.isNotEmpty(handworkDiy)){
                    //先查询一下当前DIY是否已经加入购物车
                    ShoppingCartEntity shoppingCart = shoppingCartMapper.getShoppingCartByDiyId(shoppingCartEntity.getDiyId(),shoppingCartEntity.getUserId());
                    int i = 0;
                    if(ObjectUtils.isNotEmpty(shoppingCart)){
                        //已存在该商品，只添加数量，不生成新订单
                        i = shoppingCartMapper.updateNumberByDiyId(
                                shoppingCart.getNumber()+shoppingCartEntity.getNumber()
                                ,shoppingCartEntity.getDiyId()
                                ,shoppingCartEntity.getUserId());
                    }else{
                        //不存在该商品，生成新订单
                        shoppingCartEntity.setAddToCartDate(new Date());
                        //商品总价
                        shoppingCartEntity.setTotalPrices(handworkDiy.getDiyPrice().multiply(new BigDecimal(shoppingCartEntity.getNumber())));
                        shoppingCartEntity.setDelFlag(0);
                        shoppingCartEntity.setFlag("true");
                        shoppingCartEntity.setCreateTime(new Date());
                        shoppingCartEntity.setUpdateTime(new Date());
                        i = shoppingCartMapper.insertSelective(shoppingCartEntity);
                    }
                    if (i != 0) {
                        return CommonResult.ok(EmErrorCode.SAVE_OK);
                    } else {
                        return CommonResult.error(EmErrorCode.SAVE_ERROR);
                    }
                } else {
                    return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
                }
            }
        } else {
            return CommonResult.error(EmErrorCode.PARAMETER_VALIDATION_ERROR);
        }
    }

    /**
     * @description: 用户查询购物车信息
     * @author: zq
     * @date: 2023/4/6 11:00
     * @param: [userId]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @Override
    public CommonResult shoppingCartByUserId(Integer userId) throws CommonException {
        // 判断是否userId为空
        if(ObjectUtils.isNotEmpty(userId)){
            List<Map> shoppingCartEntities = shoppingCartMapper.shoppingCartByUserId(userId);
            return CommonResult.ok(shoppingCartEntities);
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     * @description: 用户查询购物车商品数量
     * @author: zq
     * @date: 2023/4/6 20:09
     * @param: [userId]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @Override
    public CommonResult shoppingCartCountByUserId(Integer userId) throws CommonException {
        // 判断是否userId为空
        if(ObjectUtils.isNotEmpty(userId)){
            int i = shoppingCartMapper.shoppingCartCountByUserId(userId);
            // 判断购物车信息是否为空
            if (ObjectUtils.isNotEmpty(i)){
                return CommonResult.ok(i);
            }else {
                throw new CommonException(EmErrorCode.ID_NOT_OBJECT);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     * @description: 用户修改购物车商品数量方法
     * @author: zq
     * @date: 2023/4/7 8:39
     * @param: [shoppingCartModel]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @Override
    public CommonResult changeNum(ShoppingCartModel shoppingCartModel) throws CommonException  {
        // 验证参数的规范性
        if(ObjectUtils.isNotEmpty(shoppingCartModel.getId())
            &&ObjectUtils.isNotEmpty(shoppingCartModel.getNumber())
            &&ObjectUtils.isNotEmpty(shoppingCartModel.getTotalPrices())){
            ShoppingCartEntity shoppingCartEntity = new ShoppingCartEntity();
            // shoppingCartModel 对象拷贝成 shoppingCartEntity
            BeanUtils.copy(shoppingCartModel, shoppingCartEntity);
            int i = shoppingCartMapper.updateByPrimaryKeySelective(shoppingCartEntity);
            if(i > 0){
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }

    /**
     * @description: 用户删除购物车商品方法
     * @author: zq
     * @date: 2023/4/7 9:02
     * @param: [id]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @Override
    public CommonResult delShoppingCartByUserId(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            int i = shoppingCartMapper.delShoppingCartByUserId(id);
            // 判断逻辑删除是否成功
            if(i > 0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     * @description: 用户删除选中购物车商品方法
     * @author: zq
     * @date:  throws CommonException 9:23
     * @param: [id]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @Override
    public CommonResult delShoppingCart(List<Integer> listId) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(listId)){
            int i = shoppingCartMapper.delShoppingCart(listId);
            // 判断逻辑删除是否成功
            if(i > 0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     * @description: 修改选中框状态为false
     * @author: zq
     * @date: 2023/4/9 18:44
     * @param: [listId]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @Override
    public CommonResult wholeCheck(List<Integer> listId) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(listId)){
            int i = shoppingCartMapper.wholeCheck(listId);
            // 判断逻辑删除是否成功
            if(i > 0){
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     * @description: 修改选中框状态为true
     * @author: zq
     * @date: 2023/4/9 19:23
     * @param: [listId]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @Override
    public CommonResult wholeChecks(List<Integer> listId) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(listId)){
            int i = shoppingCartMapper.wholeChecks(listId);
            // 判断逻辑删除是否成功
            if(i > 0){
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     * @description: 修改单选框状态为false
     * @author: zq
     * @date: 2023/4/10 15:36
     * @param: [id]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @Override
    public CommonResult checkboxById(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            int i = shoppingCartMapper.checkboxById(id);
            // 判断逻辑删除是否成功
            if(i > 0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     * @description: 修改单选框状态为true
     * @author: zq
     * @date: 2023/4/10 15:36
     * @param: [id]
     * @return: com.zhuiYun.project.common.response.CommonResult
     **/
    @Override
    public CommonResult checkboxByIds(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            int i = shoppingCartMapper.checkboxByIds(id);
            // 判断逻辑删除是否成功
            if(i > 0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }
}
